package com.atguigu.gmall.product.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.product.model.BaseCategoryView;
import com.atguigu.gmall.product.mapper.BaseCategoryViewMapper;
import com.atguigu.gmall.product.redis.GmallCache;
import com.atguigu.gmall.product.service.BaseCategoryViewService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * VIEW 业务实现类
 *
 * @author atguigu
 * @since 2023-06-10
 */
@Service
public class BaseCategoryViewServiceImpl extends ServiceImpl<BaseCategoryViewMapper, BaseCategoryView> implements BaseCategoryViewService {

    /**
     * 查询所有1级分类列表,要求将二级分类集合包含到一级分类下, 同样三级分类
     *
     * @return
     */
    @Override
    @GmallCache(prefix = "baseCategoryList:")
    public List<JSONObject> getBaseCategoryList() {
        List<JSONObject> resultList = new ArrayList<>();
        //1.查询分类视图得到所有分类集合 共计910条记录
        List<BaseCategoryView> allBaseCategoryViewList = this.list();

        //2.对所有分类集合进行分组-按照1级分类ID(category1_id)分组 将所有一级分类放在一起 Map<某个1级分类ID值,某个1级分类集合>
        Map<Long, List<BaseCategoryView>> category1MapList = allBaseCategoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));

        //3.对分组后所有一级分类进行遍历,构建一级分类JSONObject对象
        int index = 0;
        for (Map.Entry<Long, List<BaseCategoryView>> category1MapEntry : category1MapList.entrySet()) {
            //3.1 获取一级分类ID
            Long category1Id = category1MapEntry.getKey();
            //3.2 获取一级分类名称 只需从集合中第一条记录 获取分类名称
            String category1Name = category1MapEntry.getValue().get(0).getCategory1Name();
            //3.3 构建一级分类JSONObject对象
            JSONObject category1 = new JSONObject();
            category1.put("categoryId", category1Id);
            category1.put("categoryName", category1Name);
            category1.put("index", index++);
            //4.在上面某个一级分类中,再次对当前一级分类集合按照 2级分类ID(category2_id)分组
            Map<Long, List<BaseCategoryView>> category2MapList = category1MapEntry.getValue().stream()
                    .collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
            //5.在对分组后所有二级分类进行遍历,构建二级分类JSONObject对象 封装到集合中,给1级分类子分类属性赋值
            ArrayList<JSONObject> category2List = new ArrayList<>();
            for (Map.Entry<Long, List<BaseCategoryView>> category2MapEntry : category2MapList.entrySet()) {
                //5.1 获取二级分类ID
                Long category2Id = category2MapEntry.getKey();
                //5.2 获取二级分类名称
                String category2Name = category2MapEntry.getValue().get(0).getCategory2Name();
                //5.3 构建二级分类JSONObject对象
                JSONObject category2 = new JSONObject();
                category2.put("categoryId", category2Id);
                category2.put("categoryName", category2Name);
                category2List.add(category2);
                //6.同时得到三级分类 封装到集合中,给2级分类子分类属性赋值
                List<BaseCategoryView> category2And3List = category2MapEntry.getValue();
                List<JSONObject> category3List = category2And3List.stream().map(baseCategoryView -> {
                    JSONObject category3 = new JSONObject();
                    category3.put("categoryId", baseCategoryView.getCategory3Id());
                    category3.put("categoryName", baseCategoryView.getCategory3Name());
                    return category3;
                }).collect(Collectors.toList());
                //6.2 将三级分类集合 放入二级分类对象属性 "categoryChild"
                category2.put("categoryChild", category3List);
            }
            //5.4 将二级分类集合放入一级分类对象属性"categoryChild"
            category1.put("categoryChild", category2List);
            resultList.add(category1);
        }
        return resultList;
    }
}
